package com.study.stream;

import com.study.lambda.Employee;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

/**
 *  stream是惰性操作，必须调用终止操作之后才会进行中间操作，否则不会执行中间操作
 * */
public class TestStream2 {

    List<Employee> emp= Arrays.asList(
            new Employee(101,"张三",18,9999.99),
            new Employee(102,"李四",59,6666.66),
            new Employee(103,"王五",27,3333.33),
            new Employee(104,"赵六",8,7777.77),
            new Employee(105,"田七",38,5555.55),
            new Employee(105,"田七",38,5555.55),
            new Employee(105,"田七",38,5555.55),
            new Employee(105,"田七",38,5555.55)
    );

    /**
     * filter
     * */
    @Test
    public void test1(){
        //中间操作
        Stream<Employee> employeeStream = emp.stream().filter((t) -> t.getAge() > 35);
        //终止操作
        employeeStream.forEach(System.out::println);
    }

    /**
     * limit
     * */
    @Test
    public void test2(){
        emp.stream()
                .limit(2)//获取前两个
                .forEach(System.out::println);
    }

    /**
     * skip
     * */
    @Test
    public void test3(){
        emp.stream()
                .skip(2) //跳过前两个
                .forEach(System.out::println);
    }

    /**
     * distinct
     * 通过hascode和equals来判断是否重复
     * */
    @Test
    public void test4(){
        emp.stream().distinct().forEach(System.out::println);
    }

    /**
     * 映射
     * map-接收lambda,将元素转换为其他形式或者提取信息。接收一个函数作为参数，该函数会被应用到每个元素上，并将映射成一个新元素
     * flatMap-接收一个函数作为参数，将流中每一个值都换成另一个流，然后把所有流连接成一个流
     *
     * 作用同list的add和addAll很相似
     * */

    @Test
    public void test5(){
        List<String> list=Arrays.asList("aaa","bbb","ccc","ddd");
        list.stream().map((str)->str.toUpperCase()).forEach(System.out::println);
        System.out.println("=====================================================");
        emp.stream().map((str)->str.getName()).forEach(System.out::println);
        System.out.println("=====================================================");
        emp.stream().map(Employee::getName).forEach(System.out::println);
    }

    @Test
    public void test6(){
        List<String> list=Arrays.asList("aaa","bbb","ccc","ddd");
        Stream<Stream<Character>> streamStream = list.stream().map(TestStream2::getStram);
        streamStream.forEach((str)->str.forEach(System.out::println));
        System.out.println("===============================================");
        list.stream().flatMap(TestStream2::getStram).forEach(System.out::println);
    }

    public static Stream<Character> getStram(String str){
        List<Character> characterList=new ArrayList<>();
        for(int i=0;i<str.length();i++){
            characterList.add(str.charAt(i));
        }
        return characterList.stream();
    }

    /**
     * 排序
     * sorted()-自然排序
     * sorted(Comporator com)-定制排序
     * */
    @Test
    public void test7(){
        List<String> list=Arrays.asList("ccc","aaa","bbb","ddd");
        list.stream().sorted().forEach(System.out::println);
        System.out.println("===============================");
        emp.stream().sorted((t1,t2)->{
            if(t1.getAge().equals(t2.getAge())){
                return t1.getName().compareTo(t2.getName());
            }else{
                return t1.getAge().compareTo(t2.getAge());
            }
        }).forEach(System.out::println);
    }


}
